<?php

require_once("headers.php");

if (isset($_POST[submit])) {
  if (strlen($Username) == 0) {
    echo "<p><b>Please enter a Username.</b></p>\n";
    $form="1";
  }
  elseif (strlen($_POST[password]) == 0) {
    echo "<p><b>Please enter a password.</b></p>\n";
    $form="1";
  }
  elseif (strlen($_POST[Fname]) == 0) {
    echo "<p><b>Please enter a Fname.</b></p>\n";
    $form="1";
  }
  elseif (strlen($_POST[Lname]) == 0) {
    echo "<p><b>Please enter a Lname.</b></p>\n";
    $form="1";
  }
  elseif (strlen($_POST[Email]) == 0) {
    echo "<p><b>Please enter an Email address.</b></p>\n";
    $form="1";
  }
  else {
    $Username_current=$username;
    $sql="SELECT count(*) FROM Users WHERE Username='$_POST[Username]' AND Username != '$Username_current'";
    $count=mysql_result(mysql_query($sql),0);
    if ($count == 0) {
      $sql="UPDATE Users SET Username='$_POST[Username]', password='$_POST[password]', Fname='$_POST[Fname]', Lname='$_POST[Lname]' WHERE Email='$_POST[Email]'";
      if (mysql_query($sql)) {
        echo "<p><b>Thank you for updating your details.</b></p>\n";
        if ($Username_current != $_POST[Username]) {
          echo "<p>Please <a href=\"update.php?logout\">log back in</a> to use your new Username.</p>\n";
        }
      }
      else { echo "<pre>$sql</pre>\n";
        echo "<p><b>There has been an erorr processing your request.</b></p>\n<p>Please <a href=\"javascript:history.go(-1)\">go back</a> and try again.</p>\n";
      }
    }
    else {
      echo "<p>The Username <b>$_POST[Username]</b> already exists, please enter another.</p>\n";
      $form="1";
    }
  }
}
elseif (isset($username))
{
  $Username=$username;

  $sql="SELECT * FROM Users WHERE Username='$Username'";
  $result=mysql_query($sql);
  while ($row = mysql_fetch_array($result)) {
    $id=$row[id];
    $password=$row[password];
    $Fname=$row[Fname];
    $Lname=$row[Lname];
    $Email=$row[Email];
    $form="1";
  }
}
else {
  echo "<p><b>You need to log in or <a href=\"register.php\">register</a> to use this section of the site.</b></p>\n";
}

if ($form == '1') {
  if (!isset($Username)) { $Username = $_POST[Username]; }
  if (!isset($password)) { $Username = $_POST[password]; }
  if (!isset($Email)) { $Username = $_POST[Email]; }
  if (!isset($Fname)) { $Username = $_POST[Fname]; }
  if (!isset($Lname)) { $Username = $_POST[Lname]; }

  echo "<form action=\"update.php\" method=\"post\">
	<table summary=\"Update form\" width=\"100%\">
		<tr>
			<td width=\"20%\"><label for=\"Username\">Username</label></td>
			<td width=\"80%\"><input type=\"text\" id=\"Username\" name=\"Username\" size=\"15\" value=\"$Username\" /></td>
		</tr>
		<tr>
			<td><label for=\"password\">Password</label></td>
			<td><input type=\"password\" id=\"password\" name=\"password\" size=\"15\" value=\"$password\" /></td>
		</tr>
		<tr>
			<td>E-mail</td>
			<td><b>$Email</b></td>
		</tr>
		<tr>
			<td><label for=\"Fname\">First Name</label></td>
			<td><input type=\"text\" id=\"Fname\" name=\"Fname\" size=\"15\" value=\"$Fname\" /></td>
		</tr>
		<tr>
			<td><label for=\"Lname\">Surname</label></td>
			<td><input type=\"text\" id=\"Lname\" name=\"Lname\" size=\"15\" value=\"$Lname\" /></td>
		</tr>
		<tr>
			<td></td>
			<td><br /><input type=\"hidden\" name=\"Email\" value=\"$_POST[Email]\" /><input type=\"hidden\" name=\"submit\" /><input type=\"image\" class=\"clear\" src=\"_images/form_submit.gif\" /></td>
		</tr>
	</table>
</form>\n";
}

require_once("footers.php");
?>